/* 
    Project     : 48c6c450f1a4a0cc53d9585dc0fee742
    Created on  : Mar 16, 2013, 11:29:15 PM
    Author      : Truong Khuong - khuongxuantruong@gmail.com
    Description :
        Purpose of the stylesheet follows.
*/
var _CONGIF={
    JQXGRID_ELEMENT     :   '#jqxGrid_Backend',
    _TYPE               :   'kitchen',
    BINDING_URI         :   base_url+'backend/testimony/binding/',
    EDIT_PANEL_URI      :   base_url+"backend/testimony/editpanel",
    COMMIT_URI          :   base_url+"backend/excution/commit/testimony/testimony/ttmy",
    DELETE_URI          :   base_url+"backend/excution/delete/testimony/testimony/ttmy",
    PENDING             : false
};
var _testimony=(function(){
    var init=false;
    var datafields=[
        { name  :   'ttmy_id' ,type :   'int'},
        { name  :   'ttmy_name' },
        { name  :   'ttmy_organization' },
        { name  :   'ttmy_position',type    :   'int' },
        { name  :   'ttmy_status', type : 'bool' },
        { name  :   'ttmy_insert' },
        { name  :   'ttmy_update' }
    ],
    columns=[
        { text: 'Name'     , dataField: 'ttmy_name'       ,filterable:false },
        { text: 'Organization'  , dataField: 'ttmy_organization'    ,width:220,filterable:false,columntype:'numberinput',filtertype:'number'},
        
        { text: 'Status'    , dataField: 'ttmy_status'      ,width:80,filterable:false ,columntype:'checkbox',threestatecheckbox:false,filtertype:'bool'},
        { text: 'Insert'    , dataField: 'ttmy_insert'      ,width:120,filterable:false ,cellsformat:'yyyy-MM-dd HH:mm:ss'},
        { 
            text: '#'        , dataField: 'ttmy_id'          ,width:60,filterable:false ,sortable: false,
            cellsrenderer : function (row, column, value) {
                var str="<span style='margin: 6px; float: left;'>";
                if(value && value>0){
                    try{
                        //value = $.parseJSON(value);
                        str+="\
                            <span onclick=\"_testimony.EditItem('"+value+"');\" \
                             title='Edit'><img src=\"/libraries/images/16/edit_black.png\"/></span>\
                        ";
                        str+="\
                            <span onclick=\"_testimony.DeleteItem('"+value+"');\" \
                             title='Delete'><img src=\"/libraries/images/16/delete_black.png\"/></span>\
                        ";
                    }catch(e){}
                }
                str+="</span>";
                return str;
            }
        },
    ],
    source,dataAdapter,
    grid_element=_CONGIF.JQXGRID_ELEMENT;
    return {
        OnInit:function(){
            try{
                if(init) return;
                init=true;
                //grid_element=$('#jqxGrid_Backend_Categories');
                source ={
                    datatype: "json",type : "POST",
                    datafields: datafields,
                    url:        _CONGIF.BINDING_URI,
                    filter      : function(){ $(_CONGIF.JQXGRID_ELEMENT).jqxGrid('updatebounddata', 'filter'); },
                    sort        : function(){ $(_CONGIF.JQXGRID_ELEMENT).jqxGrid('updatebounddata'); },
                    root        : 'rows',
                    beforeprocessing: function(data){ source.totalrecords = data.total_rows; }
                };
                dataAdapter = new $.jqx.dataAdapter(source, {
                    loadError: function(xhr, status, error){
                        bootbox.alert("<b>Status</b>:"+xhr.status+"<br/><b>ThrownError</b>:"+error+"<br/>");
                    }
                });
                $(_CONGIF.JQXGRID_ELEMENT).jqxGrid({
                    rendergridrows: function(obj){
                        return obj.data;    
                    },ready: function () {
                        //_testimony.Filter('cate_type',_CONGIF._TYPE);
                        
                    },
                    width: '100%',//autoheight:true,
                    source: dataAdapter,
                    theme: theme,sortable: true,
                    //filterable: true,
                    //autoshowfiltericon: true,
                    //showfilterrow: true,
                    //rowsheight:34,
                    showemptyrow: true,
                    virtualmode:false,
                    columns: columns
                });
            }catch(e){
                bootbox.alert(e.message);
            }
        },
        AddItem:function(){
            this.EditItem(0);
        },
        EditItem:function(Id){
            //if(PENDING) return false;
            //_CONGIF.PENDING=true;
            
            new backend({
                url         :   _CONGIF.EDIT_PANEL_URI,
                data        :   {
                    Id      :   Id,
                    //Type    :   _CONGIF._TYPE
                },
                callback    :   function(rsdata){
                    //_CONGIF.PENDING=false;
                    if(rsdata.result<0){
                        bckdialog({
                            message:rsdata.message,
                            type:'error'
                        }).open();
                    }else{
                        $('#_edit_panel_popup').html(rsdata.htmlreponse);
                        bckdialog({
                            title:'Testimony',message:$('#_edit_panel_popup'),
                            buttons:{
                                'Save': function() {
                                    _testimony.Save();
                                },
                                Cancel: function() {
                                    $( this ).dialog( "close" );
                                }
                            }
                        }).open();
                    }
                }
            }).call();
        },
        Save:function(){
            if(_CONGIF.PENDING) return false;
            //if( $('#category_form_edit_panel').validationEngine('validate') === false)return false;
            var Params=$('#_form_edit_panel').serializeObject();
            _CONGIF.PENDING=true;
            new backend({
                url         :   _CONGIF.COMMIT_URI,
                data        :   {
                    Params  :   Params,
                    Id      :   $('#ttmy_id').val()
                },
                callback    :   function(rsdata){
                    _CONGIF.PENDING=false;
                    if(rsdata.result<0){
                        bckdialog({
                            message:rsdata.message,
                            type:'error'
                        }).open();
                        return false;
                    }else{
                        //bootbox.alert(rsdata.message);
                        NoticeMsg(rsdata.message);
                        _testimony.Cancel();
                        _testimony.Refresh();
                        return true;
                    }
                }
            }).call();
            return false;
        },
        Cancel:function(){
            $('#_edit_panel_popup').dialog('close');
        },
        DeleteItem:function(id){
            ConfirmMsg(
                function(){
                    backend_delete('testimony','testimony','ttmy',id,function(){
                        $(_CONGIF.JQXGRID_ELEMENT).jqxGrid('updatebounddata');
                        //Example.show("Success ! Item have been Deleted.");
                    });
                }
                ,"These items will be permanently deleted and cannot be recovered. Are you sure ?"
                
            );
        },
        RestoreItem:function(){
            accessdenied();
        },
        Permission:function(id){
            
        },
        PrivateItem:function(){
            accessdenied();
        },
        Setting:function(){
            bckdialog({
                message:$('#window-sOpt'),
                icon:"<img class='p-a t-7 l-8' src='"+base_url+"/libraries/images/16/option.gif'/>",
                title:'Setting'
            }).open();
        },
        Refresh:function(){
            $(_CONGIF.JQXGRID_ELEMENT).jqxGrid('updatebounddata');
            //Msg('✔ Dép bông nhung mềm mịn - 68.000 | Bộ màu vẽ 68 món - 85.000 | Máy uốn tóc new - 135.000');
        },
        Filter:function(datafield,filtervalue){
            _CONGIF._TYPE=filtervalue;
            var filtertype = 'stringfilter';
            var filtergroup = new $.jqx.filter();
            var filter_or_operator = 1;
            var filtercondition = 'equal';
            var filter = filtergroup.createfilter(filtertype, filtervalue, filtercondition);
            filtergroup.addfilter(filter_or_operator, filter);
            
            //$(grid_element).jqxGrid('clearfilters');
            // add the filters.
            $(_CONGIF.JQXGRID_ELEMENT).jqxGrid('addfilter', datafield, filtergroup);
            // apply the filters.
            $(_CONGIF.JQXGRID_ELEMENT).jqxGrid('applyfilters');
        }
    };
}());
$(document).ready(function () {
    $('#_edit_panel').hide();
});